//controller JS
	var isPhone = isPhoneScreen();
	console.log("isPhone=",isPhone);

$(function() {
	$("#chooseMain").hide();
	if(isPhone){
		
		$(".mask").hide();
		$(".mainContext").hide();
		$(".chooseMainTitle").click(function(){
			$(".mask").show();
			$(".mainContext").show();
		})
		$(".Iknow").click(function(){
			$("#mask").hide();
			$(this).parent(".mainContext").hide();
	    })
	}
	$("#message_login_click").click(function() {
		if (!$(this).hasClass("active")) {
			return;
		}
		userMessageLogin();
	});
	$("#user_login_click").click(function() {
		if (!$(this).hasClass("active")) {
			return;
		}
		userLogin();
	});
  
});

function userMessageLogin() {
	var phone = $.trim($("#phone_input").val());
	var checkCode = $.trim($("#message_checkcode_text").val());
	if (isNull(phone)) {
		showPhoneErrMsg("请输入手机号码");
		return;
	}
	if (isNull(checkCode)) {
		showPhoneErrMsg("请输入验证码");
		return;
	}

	$.ajax({
		type : "POST",
		url : rootPath + "/userFront/accountMerge/loginbymsg",
		data : JSON.stringify({
			"login" : phone,
			"checkCode" : checkCode
		}),
		contentType : "application/json;charset=UTF-8",
		success : function(data) {
			if ("0000" == data.returnCode) {
				layer.msg("登录成功", {
					icon : 6
				});

				paint(data.returnObject);

			} else {
				showPhoneErrMsg(data.returnMessage);
			}
		}
	});
};
function userLogin() {
	var u = $("#username_input").val();
	var p = $("#password_input").val();
	var c = $("#checkcode_text").val();

	var isOk = true;
	if (isNull(u)) {
		showUserErrMsg("请输入用户名。");
		return;
		isOk = false;
	}
	if (isNull(p)) {
		showUserErrMsg("请输入密码。");
		return;
		isOk = false;
	}
	if (isNull(c)) {
		showUserErrMsg("请输入验证码。");
		return;
		isOk = false;
	}
	if (!isOk) {
		return;
	}
	console.log("post")
	$.ajax({
		type : "POST",
		url : rootPath + "/userFront/accountMerge/login",
		data : JSON.stringify({
			"un" : u,
			"pd" : p,
			"code" : c
		}),
		contentType : "application/json;charset=UTF-8",
		success : function(data) {
			if ("0000" == data.returnCode) {
				layer.msg("登录成功", {
					icon : 6
				});

				paint(data.returnObject);

			} else {
				showUserErrMsg(data.returnMessage);
			}
		}
	});
};

function paint(returnObject) {

	$("#otherUid").val(returnObject.uid);
	$("#notice").hide();
	$("#chooseMain").show();

	


	$("#layers").remove();

//	var imgSrc = rootPath + "/file/viewimg?fileName=" + returnObject.headimgurl + "&type=1";
	var imgSrc = "";
	if(returnObject != null && returnObject.headimgurl != null && returnObject.headimgurl != '') {
		imgSrc = rootPath + "/file/viewimg?fileName=" + (returnObject.headimgurl) + "&type=1";
	} else {
		imgSrc = "../../images/moren.png"
	}
	
	var group= $('<div class="group" id="main2"></div>');
	var fieldset = $('<fieldset class="account"></fieldset>');

	var imgLegend = $('<legend class="default"></legend>');

	var img = $('<img src="' + imgSrc +'">');
	img.appendTo(imgLegend);
	var username = "";
	if(typeof(returnObject.uname)!="undefined"){ 
		username = returnObject.uname;
	}
	var nicknameDiv = $('<div class="aWord">昵称: <span class="aWord">'
			+ username + '</span></div>');
	var phoneDiv = $('<div class="aWord">注册时间:<span class="aWord">'
			+ returnObject.cts + '</span></div>');
	fieldset.appendTo(group);
	imgLegend.appendTo(fieldset);
	nicknameDiv.appendTo(fieldset);
	phoneDiv.appendTo(fieldset);
	
	group.appendTo($("#acountMerge"));
	var choose = $('<label class="current-next"><input type="radio" style=" margin-right: 10px;" name="choose" value="my" checked="checked">保留此账号（当前账号） </label>');
	var c = $('<label class="current-next"><input type="radio" style=" margin-right: 10px;" name="choose" value="other">保留此账号</label>');
	$("#main1").prepend(choose);
	$("#main2").prepend(c);

	// 合并按钮
	var mergeButton = $('<div class="commitBtn"><input type="button" id="merge" value="申请合并" onclick="merge();" ></div>');
	mergeButton.appendTo($('#contentInfo'));
	if(isPhone){
		$(".account").show();
		/*$(".chooseMainTitle").click(function(){
			$(".mask").show();
			$(".mainContext").show();
		})
		$(".Iknow").click(function(){
			$("#mask").hide();
			$(this).parent(".mainContext").hide();
	    })*/
		
		
		
	}
};

function merge(){
	var otherUid = $("#otherUid").val();
	var choose = $("input[name='choose']:checked").val();
	if(isNull(otherUid)||isNull(choose)){
		layer.msg("数据错误，刷新重试", {
			icon : 2
		});
		return;
	}
	$.ajax({
		type : "POST",
		url : rootPath + "/user/mergeApplication",
		data : JSON.stringify({
			"otherUid" : otherUid,
			"choose" : choose
		}),
		contentType : "application/json;charset=UTF-8",
		success : function(res) {
			if ("0000" == res.returnCode) {
				layer.msg("申请成功", 
						{
							icon : 6,
							time: 2000
						},
						function(){
							parent.$("#content").prop("src","securitySetting.html");
				});
				

			} else {
				layer.msg(res.returnMessage, 
						{
							icon : 2
						}); 
			}
		}
		
	});
};


